package cn.meike.net
{
	import cn.meike.net.protocol.KylinMsg;
	import cn.meike.net.protocol.LoginServer_Request;
	import cn.meike.net.protocol.Macros;
	
	import flash.utils.ByteArray;

	/**
	 * 具体的项目的协议工厂类，跟具体的项目相关
	 * @author
	 * 
	 */	
	public class MsgFactory
	{
		
		public static function getLoginServer_Request(uin:uint, seq:uint, skey :String = "", loginCookie: String = ""):KylinMsg
		{
			var msg:KylinMsg = new KylinMsg();
			//msg.m_stMsgHead.m_shMsgVersion = 1;
			//msg.m_stMsgHead.m_uiTimeStamp =0;
			msg.m_stMsgHead.m_uiUin = uin;
			msg.m_stMsgHead.m_uiMsgID = Macros.MsgID_LoginServer_Request;
			
			_makeHeadSkey(msg, skey);

			var loginReq:LoginServer_Request = new LoginServer_Request();
			loginReq.m_uiUin = uin;
			loginReq.m_uiSeq = seq;
			
			msg.m_stMsgBody.m_stLoginServer_Request = loginReq;
			
			
			return msg;
		}
		
		private static function _makeHeadSkey(msg: KylinMsg, skey: String): void
		{
//			CONFIG::debug
//			{
//				assert(msg != null, "参数必须合法");
//			}
			
			//Log.trace("MsgID_Account_ListRole_Request: skey is " + skey, Log.TFLOG);
			var skeyArr: ByteArray = new ByteArray();
			if (skey != null && skey != "")
			{
				skeyArr.writeUTFBytes(skey);
				msg.m_stMsgHead.m_ucSKeyLength = skey.length;
			}
			else
			{
				msg.m_stMsgHead.m_ucSKeyLength = 0;
			}
			
			msg.m_stMsgHead.m_szSessionKey = new Vector.<uint>(msg.m_stMsgHead.m_ucSKeyLength);
			for (var i :int; i < skeyArr.length; ++i)
			{
				if (i == msg.m_stMsgHead.m_szSessionKey.length)
				{
					// 越界...
					//Log.trace("Error getRoleList: m_szSessionKey 越界！");
					break;
				}
				msg.m_stMsgHead.m_szSessionKey[i] = skeyArr[i];
			}
		}
	}
}